package com.atguigu.gmall.item.test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: gmall-parent
 * @author: Mr.Zhuo
 * @create: 2022-04-08 14:48
 **/
public class CompletableFutureDemo01 {

    public static void main(String[] args) {
        // 创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                9,
                500,
                30,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20));

        CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> {
            return "打飞机";
        }, threadPoolExecutor);

        // 线程2 获取到线程1执行的结果
        CompletableFuture<Void> futureB = futureA.thenAcceptAsync((s) -> {
            delaySec(3);
            printCurrTime(s + " 第一个线程");
        }, threadPoolExecutor);

        CompletableFuture<Void> futureC = futureA.thenAcceptAsync((s) -> {
            delaySec(1);
            printCurrTime(s + " 第二个线程");
        }, threadPoolExecutor);

        threadPoolExecutor.shutdown();
    }

    private static void printCurrTime(String str) {
        System.out.println(str);
    }

    private static void delaySec(int i) {
        try {
            Thread.sleep(i * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
